// 通用扁平化方法 - 指定扁平化字段
export function customBaseFlatten(array, tar = 'children', depth = 999,
	predicate = Array.isArray, result = []) {
	if (array == null) {
		return result
	}
	for (const value of array) {
		if (depth > 0 && predicate(value[tar])) {
			if (depth > 1) {
				// 递归调用, 深度-1
				// Recursively flatten arrays (susceptible to call stack limits).
				const temp = JSON.parse(JSON.stringify(value))
				temp[tar] = undefined
				result.push(temp)
				customBaseFlatten(value[tar], tar, depth - 1, predicate, result)
			} else {
				// 未达到指定深度展开当前一层
				result.push(...value)
			}
		} else {
			// 一般条件
			result[result.length] = value
		}
	}
	return result
}
export function debounce(fn, delay = 500) {
	let timer = null //记录上一次延时器
	return function() {
		if (timer) clearTimeout(timer); //清除上一次定时器
		// 重新设置定时器
		timer = setTimeout(function() {
			fn();
		}, delay);
	}
}
// 判断是否具有对应菜单权限
export function confimMenuExist(menus, tar) {
	const list = customBaseFlatten(menus)
	return list.find(x => x.id === tar.id)
}